﻿using DbFactory;
using PlaneClean.Models;
using PlaneClean.Models.Menu;
using PlaneClean.Service;
using PlaneClean.ViewModels;
using PlaneCleanEntity;
using PlaneCleanEntity.ViewEntity.RouterEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace PlaneClean.Controllers.ManagerController
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public class MenuController : ManagerController
    {

        /// <summary>
        /// 查询所有菜单树
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ListResponse<v_t_menu> List(PageListRequest<t_menu> request)
        {
            ListResponse<v_t_menu> response = new ListResponse<v_t_menu>();
            response.bodyList = new List<v_t_menu>();
            MenuSerivce ms = new MenuSerivce();
            //var sugar = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_state == 1);
            var sugar = dbFactory.Db.Queryable<t_menu>();
            if (!string.IsNullOrEmpty(request.body.m_name))
            {
                sugar.Where(a => a.m_name == request.body.m_name);
                List<v_t_menu> menuList = sugar.OrderBy(a => a.m_sequence).Select<v_t_menu>().ToList();
                response.bodyList = ms.ToTreeMenu(menuList, menuList[0].m_parent_id ?? 0);
            }
            else
            {
                List<v_t_menu> menuList = sugar.OrderBy(a => a.m_sequence).Select<v_t_menu>().ToList();

                response.bodyList = ms.ToTreeMenu(menuList, 0);
            }

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Add(BaseRequest<t_menu> request)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";
            //if (request.body == null)
            //{
            //    response.reason = "请求body不能为空";
            //    return response;
            //}
            if (request.body.m_id > 0)
            {
                response.reason = "有主键请调用更新接口";
                return response;
            }
            if (request.body.m_parent_id == null)
            {
                request.body.m_parent_id = 0;
            }
            int pId = DbFactory.dbFactory.Db.Insertable(request.body).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity();
            if (pId <= 0)
            {
                response.reason = "插入数据库异常,新增失败";
                return response;
            }

            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Update(BaseRequest<t_menu> request)
        {
            BaseResponse response = new BaseResponse();
            response.result = "NG";

            if (request.body.m_id <= 0)
            {
                response.reason = "没有主键请调用新增接口";
                return response;
            }
            if (request.body.m_parent_id == null)
            {
                request.body.m_parent_id = 0;
            }
            t_menu menu = DbFactory.dbFactory.Db.Queryable<t_menu>().First(a => a.m_id == request.body.m_id);
            if (menu == null)
            {
                response.reason = "需要更新的记录不存在";
                return response;
            }

            if (request.body.m_state != null)
            {
                t_menu Childmenu = new t_menu();
                List<t_menu> brotherMenuList = new List<t_menu>();
                if (request.body.m_state <= 0)
                {
                    if (menu.m_editable == 0)
                    {
                        response.reason = "菜单:" + menu.m_name + ",不可禁用/删除";
                        return response;
                    }
                    Childmenu = dbFactory.Db.Queryable<t_menu>()
                        .Where(a => a.m_parent_id == menu.m_id && a.m_state > 0).First();
                    if (Childmenu != null)
                    {
                        response.reason = menu.m_name + ",存在启用的子菜单,不可禁用/删除";
                        return response;
                    }
                    DbFactory.dbFactory.Db.Updateable(request.body).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
                    brotherMenuList = dbFactory.Db.Queryable<t_menu>()
                        .Where(a => a.m_parent_id == menu.m_parent_id && a.m_state > 0).ToList();
                    if (brotherMenuList == null || brotherMenuList.Count <= 0)
                    {
                        DbFactory.dbFactory.Db.Updateable<t_menu>()
                            .SetColumns(a => a.m_state == 0).Where(a => a.m_id == menu.m_parent_id).ExecuteCommand();
                    }
                }
                else
                {
                    Childmenu = dbFactory.Db.Queryable<t_menu>()
                        .Where(a => a.m_parent_id == menu.m_id && a.m_state <= 0).First();
                    if (Childmenu != null)
                    {
                        response.reason = menu.m_name + ",存在禁用/删除的子菜单,不可启用";
                        return response;
                    }
                    DbFactory.dbFactory.Db.Updateable(request.body).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
                    brotherMenuList = dbFactory.Db.Queryable<t_menu>()
                   .Where(a => a.m_parent_id == menu.m_parent_id && a.m_state > 0).ToList();
                    if (brotherMenuList != null && brotherMenuList.Count > 0)
                    {
                        DbFactory.dbFactory.Db.Updateable<t_menu>()
                            .SetColumns(a => a.m_state == 1).Where(a => a.m_id == menu.m_parent_id).ExecuteCommand();
                    }
                }
            }
            else
            {
                DbFactory.dbFactory.Db.Updateable(request.body).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
            }




            response.result = "OK";
            return response;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public BaseResponse Delete(BaseDeleteRequest request)
        {
            BaseResponse response = new BaseResponse();

            if (request.idList == null || request.idList.Count <= 0)
            {
                response.reason = "请传入需要删除数据的主键";
                return response;
            }
            List<t_menu> menuList = DbFactory.dbFactory.Db.Queryable<t_menu>().Where(a => request.idList.Contains(a.m_id)).Where(a => a.m_editable == 0).ToList();
            if (menuList != null && menuList.Count > 0)
            {
                response.reason = "菜单:" + menuList[0].m_name + ",不可删除";
                return response;
            }
            foreach (var mid in request.idList)
            {
                t_menu Childmenu = dbFactory.Db.Queryable<t_menu>().Where(a => a.m_parent_id == mid).First();
                if (Childmenu != null)
                {
                    t_menu menu = dbFactory.Db.Queryable<t_menu>().Where(a => mid == a.m_id).First();
                    response.reason = menu.m_name + ",存在子菜单,删除失败";
                    return response;
                }
            }



            DbFactory.dbFactory.Db.Deleteable<t_menu>().In(request.idList).ExecuteCommand();
            dbFactory.Db.Deleteable<t_group_menu>().Where(a => request.idList.Contains(a.m_id)).ExecuteCommand();
            response.result = "OK";
            return response;
        }

    }
}